package com.ssl.springframework.beans.factory.support;

import com.ssl.springframework.beans.factory.BeanFactory;
import com.ssl.springframework.beans.factory.config.BeanDefinition;

/**
 * 描述：抽象bean工厂
 *
 * @author ssl
 * @create 2021/6/10 0010.
 */
public abstract class AbstractBeanFactory extends DefaultSignletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String name) {
        // 从单例缓存池中获取
        Object bean = getSingleton(name);
        if (null != bean) {
            // 获取到直接返回
            return bean;
        }
        // 未获取到，创建
        BeanDefinition beanDefinition = getBeanDefinition(name);

        return createBean(name, beanDefinition);
    }

    /**
     * 创建Bean
     *
     * @param name
     * @param beanDefinition
     * @return
     */
    protected abstract Object createBean(String name, BeanDefinition beanDefinition);

    /**
     * 获取bean定义
     *
     * @param name
     * @return
     */
    protected abstract BeanDefinition getBeanDefinition(String name);
}
